package org.zbclan.bf2.rcon.common.server;

import net.customware.gwt.dispatch.server.guice.ActionHandlerModule;
import net.customware.gwt.dispatch.server.guice.GuiceStandardDispatchServlet;

import org.zbclan.bf2.rcon.home.server.GetGameModeListHandler;
import org.zbclan.bf2.rcon.home.server.GetGamePlayObjectHandler;
import org.zbclan.bf2.rcon.home.server.GetLevelNameListHandler;
import org.zbclan.bf2.rcon.home.server.SaveGamePlayObjectHandler;
import org.zbclan.bf2.rcon.home.shared.GetGameModeListCommand;
import org.zbclan.bf2.rcon.home.shared.GetGamePlayObjectCommand;
import org.zbclan.bf2.rcon.home.shared.GetLevelNameListCommand;
import org.zbclan.bf2.rcon.home.shared.SaveGamePlayObjectCommand;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;

/**
 * @author renfeng
 * 
 */
public class GuiceServletConfig extends GuiceServletContextListener {

	@Override
	protected Injector getInjector() {

		ActionHandlerModule actionHandlerModule = new ActionHandlerModule() {

			@Override
			protected void configureHandlers() {
				bindHandler(GetLevelNameListCommand.class,
						GetLevelNameListHandler.class);
				bindHandler(GetGameModeListCommand.class,
						GetGameModeListHandler.class);
				bindHandler(GetGamePlayObjectCommand.class,
						GetGamePlayObjectHandler.class);
				bindHandler(SaveGamePlayObjectCommand.class,
						SaveGamePlayObjectHandler.class);
			}
		};

		ServletModule servletModule = new ServletModule() {

			@Override
			protected void configureServlets() {

				// /*
				// * TODO deprecate
				// */
				// serve("/gwt.levellist/level").with(LevelServiceImpl.class);
				// bind(LevelServiceImpl.class).in(Singleton.class);

				serve("*/dispatch").with(GuiceStandardDispatchServlet.class);

				// serve("*/gwt-log").with(RemoteLoggerServiceImpl.class);
				// bind(RemoteLoggerServiceImpl.class).in(Singleton.class);

				// serve("/levels/*/info").with(LevelInfoServlet.class);
				// serve("/info").with(LevelInfoServlet.class);

				return;
			}
		};

		AbstractModule customModule = new AbstractModule() {

			@Override
			protected void configure() {

				// bind(Log.class).toProvider(LogProvider.class).in(
				// Singleton.class);

				// bind(File.class).annotatedWith(
				// Names.named(Options.BF2_LEVELS_DIR)).toInstance(
				// new File(System.getProperty(Options.BF2_LEVELS_DIR)));

				bind(ServerModel.class).in(Singleton.class);

			}
		};

		return Guice.createInjector(actionHandlerModule, servletModule,
				customModule);
	}
}
